package com.adnaan.backend.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.SystemMessage;
import com.adnaan.backend.service.SystemMessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@RestControllerMapping("/message")
@Tag(name = "消息管理", description = "消息管理")
public class SystemMessageController {

    @Resource
    private SystemMessageService systemMessageService;

    /**
     * 获取最新的三条未读消息
     */
    @GetMapping("/latest")
    @Operation(summary = "获取最新的三条未读消息")
    public Result<List<SystemMessage>> getLatestMessages() {
        return Result.success(systemMessageService.getLatestUnreadMessages(StpUtil.getLoginIdAsLong(), 3));
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count")
    @Operation(summary = "获取未读消息数量")
    public Result<Long> getUnreadCount() {
        return Result.success(systemMessageService.getUnreadCount(StpUtil.getLoginIdAsLong()));
    }

    /**
     * 分页获取消息列表
     */
    @PostMapping("/page")
    @Operation(summary = "分页获取消息列表")
    public Result<Page<SystemMessage>> getMessages(@RequestBody PageParams<SystemMessage> pageParams) {
        return Result.success(systemMessageService.getUserMessages(StpUtil.getLoginIdAsLong(), pageParams.getPage()));
    }

    /**
     * 标记消息为已读
     */
    @GetMapping("/read/{messageId}")
    @Operation(summary = "标记消息为已读")
    public Result<Boolean> markAsRead(@PathVariable Long messageId) {
        systemMessageService.markAsRead(messageId);
        return Result.success(true);
    }

    /**
     * 标记所有消息为已读
     */
    @GetMapping("/read-all")
    @Operation(summary = "标记所有消息为已读")
    public Result<Boolean> markAllAsRead() {
        systemMessageService.markAllAsRead(StpUtil.getLoginIdAsLong());
        return Result.success(true);
    }
} 